import { MainUI } from "./MainUI.js";
import { tt } from "../ttapi_interface/ttapi.js";
import * as tt2 from "../ttapi_layer2/ttlayer2.js";
import * as EUI from "./eui/EUI.js"
import { EditorManager } from "./fileeditor/editor.js";
import { EUI_Dialog_Input } from "./eui/eui_dialog_input.js";

export class MainUI_Menu {

    static _InitMenu(mainui: MainUI): EUI.EUI_MenuBar {

        let title = EUI.EUI_Style.CreateLabel("TTEditor V5");
        title.cut = true;
        title.halign = tt2.QUI_HAlign.Left;
        title.localRect.setHPosByLeftBorder(130);
        title.localRect.setVPosByTopBorder(14, 1);

        mainui.window.back.addChild(title);
        let menu = mainui.menu = new EUI.EUI_MenuBar(mainui.window, mainui.font, mainui.canvas);
        mainui.menu.localRect.setHPosFill(130, 0);
        mainui.menu.localRect.setVPosByTopBorder(18, 0);
        mainui.menu.backElement = EUI.EUI_Style.CreateDefaultBack();
        mainui.window.back.addChild(mainui.menu);






        menu.AddMenuItem("文件/打开工作区", async () => {


            let uidialog = new EUI.EUI_Dialog_Folder();


            let filedir = await uidialog.Show(mainui.window);
            console.log("filedir=" + filedir);
            if (filedir != null) {

                mainui.filetree.SetRoot(filedir);
            }

        });
        mainui.menu.AddMenuItem("文件/刷新工作区", () => {
            let _root = mainui.filetree.getRoot();
            if (_root != null) {
                mainui.filetree.SetRoot(_root);
            }
        });

        mainui.menu.AddMenuItem("文件/_");

        mainui.menu.AddMenuItem("文件/新建", () => {
            let dialog = new EUI.EUI_Dialog_Msgbox();
            dialog.Show(mainui.window, "not impl yet.");
        });
        this._InitMenu_New(mainui);

        mainui.menu.AddMenuItem("文件/重命名", () => {
            let dialog = new EUI.EUI_Dialog_Msgbox();
            dialog.Show(mainui.window, "not impl yet.");
        });
        mainui.menu.AddMenuItem("文件/删除", () => {
            let dialog = new EUI.EUI_Dialog_Msgbox();
            dialog.Show(mainui.window, "not impl yet.");
        });

        mainui.menu.AddMenuItem("文件/_");


        mainui.menu.AddMenuItem("文件/关于", () => {
            let msgdialog = new EUI.EUI_Dialog_Msgbox();
            msgdialog.Show(mainui.window, "你点击了关于，但是我们还啥都没写，哈哈");
            //console.warn("你点击了关于，但是我们还啥都没写，哈哈");

        });
        mainui.menu.AddMenuItem("剪切板/清除所有");
        mainui.menu.AddMenuItem("剪切板/_");
        mainui.menu.RefreshMenu();

        return menu;
    }
    static _InitMenu_New(mainui: MainUI) {
        let aee = EditorManager.GetAllEditors();
        for (var i = 0; i < aee.length; i++) {
            if (aee[i].supportNewFile() == false)
                continue;

            let name = aee[i].getName();
            let editr = aee[i];
            mainui.menu.AddMenuItem("文件/新建/" + name + "(" + aee[i].getCareExts()[0] + ")", async () => {

                let pickfolder = mainui.filetree.getPickFolder();
                if (pickfolder == null)
                    return;
                let fd = new EUI_Dialog_Input();
                let r = await fd.Show(mainui.window, "输入文件名", 16);
                if (r.result) {
                    let filename = r.text;
                    let _ext = editr.getCareExts()[0];
                    if (filename.toLowerCase().indexOf(_ext) > 0) {

                    }
                    else {
                        filename += _ext;
                    }
                    console.log("create file:" + filename + " at:" + pickfolder);
                    //need NewFileDialog 缺 对话框
                    let b = await editr.newFileAsync(pickfolder + "/" + filename, mainui.window);

                    if (b) {
                        //刷一下UI
                        let _root = mainui.filetree.getRoot();
                        if (_root != null) {
                            mainui.filetree.SetRoot(_root);
                        }
                        mainui.filetree.PickByFullName(pickfolder + "/" + filename);
                        // this.filetree.PickByFullName(this.filetree); //缺 从文件树选文件
                    }
                }
                else {
                    console.log("取消 new file.");
                }
            });
        }
    }
}